#!/bin/sh
### BEGIN INIT INFO
# Provides:          servoblaster
# Required-Start:    hostname $local_fs
# Required-Stop:
# Should-Start:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop servod.
# Description:       This script starts/stops servod.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh

OPTS="--idle-timeout=2000"

STATUSFILE="/tmp/servoblaster-status"

if [ $( id -u ) != 0 ]; then
	echo "ERROR: Must be run as root"
	exit 1
fi

case "$1" in
  start)
	/usr/local/sbin/servod $OPTS >/dev/null
	;;
  restart|reload|force-reload)
	killall servod
	/usr/local/sbin/servod $OPTS >/dev/null
	;;
  stop)
	killall servod
	;;
  status)
	if [ ! -e /dev/servoblaster ]; then
		echo "ERROR: /dev/servoblaster does not exist"
		exit 2
	fi
	rm -f $STATUSFILE
	echo "status $STATUSFILE" > /dev/servoblaster
	sleep 0.2
	if [ ! -e $STATUSFILE ]; then
		echo "ERROR: servod not responding"
		exit 3
	elif grep -q "^OK" $STATUSFILE; then
		echo "OK"
		exit 0
	elif grep "^ERROR:" $STATUSFILE; then
		exit 4
	else
		echo "ERROR: No status from servod"
		exit 5
	fi
	;;
  *)
	echo "Usage: servoblaster [start|stop|status]" >&2
	exit 6
	;;
esac

exit 0

